package com.softline.documentsmall.service.impl;


import com.google.common.collect.Maps;
import com.softline.documentsmall.dao.HotIssuesMapper;
import com.softline.documentsmall.dao.IssuesAnswerMapper;
import com.softline.documentsmall.entity.HotIssues;
import com.softline.documentsmall.entity.IssuesAnswer;
import com.softline.documentsmall.ret.BusinessErrorEnum;
import com.softline.documentsmall.ret.ReturnVO;
import com.softline.documentsmall.ret.ReturnVOUtils;
import com.softline.documentsmall.service.HotIssuesService;
import com.softline.documentsmall.service.IssuesAnswerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author zhongweicheng
 * @date 2020/8/25 15:46
 */
@Service
@Slf4j
public class IssuesAnswerServiceImp implements IssuesAnswerService {

    @Value(value = "${pageParams.currPage}")
    private Integer currPageCfg;

    @Value(value = "${pageParams.pageSize}")
    private Integer pageSizeCfg;



    @Autowired
    IssuesAnswerMapper IssuesAnswerMapper;

    /**
     * 按照条件组合查询回答列表
     * @param id
     * @param issuesId
     * @param content
     * @param passportId
     * @param approveState
     * @param isDelete
     * @param currPage
     * @param pageSize
     * @param startDate
     * @param endDate
     * @return
     */
    @Override
    public ReturnVO findByParams(Integer id, Integer issuesId, String content, String passportId, Integer approveState, Integer isDelete, Integer currPage, Integer pageSize, String startDate, String endDate) {

        //分页参数,不传则使用默认值
        if(Objects.isNull(currPage)) {
            currPage = currPageCfg;
        }
        if(Objects.isNull(pageSize)) {
            pageSize = pageSizeCfg;
        }

        return ReturnVOUtils.success(IssuesAnswerMapper.findByParams(id,issuesId,content,passportId,approveState,isDelete, (currPage-1)*pageSize,pageSize,startDate,endDate));
    }


    /**
     * 新增回答
     * @param hotIssues
     * @return
     */
    @Override
    public ReturnVO insertIssueAnswer(IssuesAnswer hotIssues) {
        try{
           IssuesAnswerMapper.insertIssueAnswer(hotIssues);
           return ReturnVOUtils.success();
        }catch (Exception e){
            log.error(" insertIssueAnswer exception{}",e.getMessage());
        }
        return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
    }

    /**
     * 根据id更新，全量传递数据
     * @param hotIssues
     * @return
     */
    @Override
    public ReturnVO updateIssueAnswerById(IssuesAnswer hotIssues) {
        try{
            //查询状态 只有1审批通过，2审批不通过才可以进行更新
            Integer id = hotIssues.getId();
            if(Objects.nonNull(id)) {
                Integer as =   IssuesAnswerMapper.findApproveStateById(id);
                if(Objects.nonNull(as)) {
                    if(as != 0){
                    //待审核状态才可以更新
                    IssuesAnswerMapper.updateIssueAnswerById(hotIssues);
                    return ReturnVOUtils.success();
                    }else{
                        return ReturnVOUtils.businessFail(BusinessErrorEnum.STATE_NOT_ALLOW_UPDATE);
                    }
                }else {
                    return ReturnVOUtils.businessFail(BusinessErrorEnum.ID_NOT_FOUND);
                }
            }else {
                log.error("updateIssueAnswerById : id is null!!");
                return ReturnVOUtils.businessFail(BusinessErrorEnum.ID_IS_NULL);
            }
        }catch (Exception e){
            log.error(" updateIssueAnswerById exception{}",e.getMessage());
        }
        return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);

    }

    /**
     * 根据id软删除问题
     * @param id
     */
    @Override
    public ReturnVO softDeleteIssueAnswerById(Integer id) {
        try{
            if(Objects.nonNull(id)) {
                Integer as = IssuesAnswerMapper.findApproveStateById(id);
                if(Objects.nonNull(as)) {
                    if(as != 0) {
                        //查询状态 处于提交中不允许删除
                        IssuesAnswerMapper.softDeleteIssueAnswerById(id);
                        return ReturnVOUtils.success();
                    }else{
                        return ReturnVOUtils.businessFail(BusinessErrorEnum.STATE_NOT_ALLOW_UPDATE);
                    }
                }else {
                    return ReturnVOUtils.businessFail(BusinessErrorEnum.ID_NOT_FOUND);
                }
            }else {
                return ReturnVOUtils.businessFail(BusinessErrorEnum.ID_IS_NULL);
            }

        }catch (Exception e){
            log.error(" softDeleteIssueAnswerById exception{}",e.getMessage());
        }
        return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
    }

    /**
     * 根据回答id更新审核状态
     * @param id
     * @param apporveState
     * @return
     */
    @Override
    public ReturnVO updateApproveStateById(Integer id, Integer apporveState) {
        if(Objects.nonNull(id) && Objects.nonNull(apporveState)){
            try{
                //拼装map
                Map<String,Object> map = Maps.newHashMap();
                map.put("id",id);
                map.put("approveState",apporveState);
                //调用mapper
                IssuesAnswerMapper.updateApproveStateById(map);
                return ReturnVOUtils.success();
            }catch (Exception e){
                log.error(" updateApproveStateById exception{}",e.getMessage());

            }
        }else {
            log.warn("updateApproveStateById has null params!!");
            return ReturnVOUtils.businessFail(BusinessErrorEnum.PARAMS_HAS_NULL);
        }
        return ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);

    }


    /**
     * 根据问题id批量软删除回答列表
     * @param issuesId
     * @return
     */
    @Override
    public ReturnVO softDeleteIssueAnswerByIssueId(Integer issuesId) {

        try{
            IssuesAnswerMapper.softDeletIssueAnswerByIssueId(issuesId);
            return ReturnVOUtils.success();
        }catch (Exception e){
            log.warn("softDeleteIssueAnswerByIssueId exception!!");

        }
        return  ReturnVOUtils.businessFail(BusinessErrorEnum.FAIL);
    }

    /**
     * 根据回答的id获取该记录的审批状态 0未审批，1审批通过，2审批不通过
     * @param id
     * @return
     */
    @Override
    public Integer findApproveStateById(Integer id) {
        return IssuesAnswerMapper.findApproveStateById(id);
    }
}
